import PhraseOWordUsage from "@/models/PhraseOWordUsage";
import {pick,assignIn} from "lodash-unified";
import 'reflect-metadata'
import {Type,Exclude, Expose} from "class-transformer";
import {clearAndPushAll} from "@/utils/ArrayUtil";
import {result} from "lodash";
import { v4 as uuidv4 } from 'uuid';

class Liju {

    @Expose()
    get eng() {
        return this._eng;
    }

    @Expose()
    set eng(value) {
        this._eng = value;
    }

    @Expose()
    get chi() {
        return this._chi;
    }

    @Expose()
    set chi(value) {
        this._chi = value;
    }

    constructor(eng, chi) {

        this._eng = eng;
        this._chi = chi;
    }
}

class YoudaoLiju extends Liju {
    constructor(eng, chi, secondary) {
        super(eng, chi);
        this._secondary = secondary;
    }


    @Expose()
    get secondary() {
        return this._secondary;
    }

    @Expose()
    set secondary(value) {
        this._secondary = value;
    }
}

// @Exclude
// class YoudaoLijuPatternMarkingTemp extends YoudaoLiju {
class YoudaoLijuPatternMarkingTemp {



    // @Type(()=>PhraseOWordUsage)
    // _phraseOWordUsage;


    /**
     *
     * @private PhraseOWordUsage[]
     */
    @Type(()=>PhraseOWordUsage.PhraseOWord)
    _pickedPhrasesTopMost;

    @Type(()=>PhraseOWordUsage.PhraseOWord)
    _pickedPhrases1

    /**
     *
     * @private PhraseOWordUsage[]
     */
    @Type(()=>PhraseOWordUsage.PhraseOWord)
    _pickedWordsTopMost;

    _uniqueId;

    constructor(eng, chi, secondary) {
        // super(eng, chi, secondary);
        this._engHiliParts = new YoudaoLijuPatternMarkingTemp.HilightParts(eng);
        this._chiHiliParts = new YoudaoLijuPatternMarkingTemp.HilightParts(chi);
        this._pickDoneFormsCountCheckedListMap = new Map();//已选定的formcounts
        this._formsCountCheckedListMap = new Map();//全量重选的formcounts
        this._formsCountTempPickListMap = new Map();

        //deletePickDoneFormsCountsListMap:{
        // "v":true,"adj":true}
        this._batchModifyPendingConfirm = {
            toAddFormsCountsListMap:new Map(),
            toOverWriteFormsCountsListMap:new Map(),
        };

        this._youdaoLiju = new YoudaoLiju(eng,chi,secondary);
        // this._phraseOWordUsage = new PhraseOWordUsage(this._youdaoLiju);
        this.phrasingIt = false;
        this._pickedPhrasesTopMost = new PhraseOWordUsage.PhraseOWordArray();
        this._pickedPhrases1 = new PhraseOWordUsage.PhraseOWordArray();
        this._pickedWordsTopMost = new PhraseOWordUsage.PhraseOWordArray();

        this._uniqueId = uuidv4();
    }


    get uniqueId() {
        return this._uniqueId;
    }

    set uniqueId(value) {
        this._uniqueId = value;
    }

    toPersistRaw(){
        let resultObj = {};
        assignIn(resultObj,this);
        resultObj._pickDoneFormsCountCheckedListMap = Object.fromEntries(this._pickDoneFormsCountCheckedListMap);
        // resultObj._phraseOWordUsage = this._phraseOWordUsage.toPersistRaw();
        // resultObj._sss="ssss";
        return pick(resultObj,['_eng','_chi','_secondary','_pickDoneFormsCountCheckedListMap']);
    }


    get eng() {
        return this._youdaoLiju.eng;
    }

    // set eng(value) {
    //     super.eng = value;
    // }

    get chi() {
        return this._youdaoLiju.chi;
    }

    // set chi(value) {
    //     super.chi = value;
    // }

    get secondary() {
        return this._youdaoLiju.secondary;
    }

    // set secondary(value) {
    //     super.secondary = value;
    // }

    @Expose()
    get youdaoLiju() {
        return this._youdaoLiju;
    }

    @Type(()=>YoudaoLiju)
    set youdaoLiju(value) {
        this._engHiliParts.originText=value.eng;
        this._chiHiliParts.originText=value.chi;
        this._youdaoLiju = value;
    }

    // @Expose()
    // get phraseOWordUsageTopMost() {
    //     return this._phraseOWordUsage;
    // }
    //
    // @Type(()=>PhraseOWordUsage)
    // set phraseOWordUsageTopMost(value) {
    //     // value.eng = this.eng;
    //     // value.chi = this.chi;
    //     // console.log('this._youdaoLiju:',this._youdaoLiju)
    //     value.pickedPhrasesOWords.forEach(pickedPhraseOWord=>pickedPhraseOWord.liju = this._youdaoLiju);
    //     value.liju = this._youdaoLiju;
    //     // console.log("setting phraseOWordUsageTopMost:",value)
    //     this._phraseOWordUsage = value;
    // }

    @Expose()
    get pickedPhrasesTopMost() {
        return this._pickedPhrasesTopMost;
    }

    @Type(()=>PhraseOWordUsage.Phrase)
    set pickedPhrasesTopMost(value) {
        value.forEach(thePickedPhrase=>{
            thePickedPhrase.liju=this._youdaoLiju;
            thePickedPhrase?.subPhrases?.forEach(subPhrase=>subPhrase.liju=new Liju(thePickedPhrase.phraseOWordText));
        });
        clearAndPushAll(this._pickedPhrasesTopMost,[...value])
    }


    @Expose()
    get pickedPhrases1() {
        return this._pickedPhrases1;
    }

    @Type(()=>PhraseOWordUsage.Phrase)
    set pickedPhrases1(value) {
        this._pickedPhrases1 = value;
    }

    @Expose()
    get pickedWordsTopMost() {
        return this._pickedWordsTopMost;
    }

    @Type(()=>PhraseOWordUsage.Word)
    set pickedWordsTopMost(value) {
        clearAndPushAll(this._pickedWordsTopMost,value);
    }

    get batchModifyPendingConfirm() {
        return this._batchModifyPendingConfirm;
    }

    clearBatchModifyPendingConfirmAll(){
        this._batchModifyPendingConfirm.toAddFormsCountsListMap.clear();
        this._batchModifyPendingConfirm.toOverWriteFormsCountsListMap.clear();
    }

    get formsCountTempPickListMap() {
        return this._formsCountTempPickListMap;
    }

    set formsCountTempPickListMap(value) {
        this._formsCountTempPickListMap = value;
    }

    get formsCountCheckedListMap() {
        return this._formsCountCheckedListMap;
    }

    // set formsCountCheckedListArr(value) {
    //     this._formsCountCheckedListArr = value;
    // }

    get engHiliParts() {
        return this._engHiliParts;
    }

    set engHiliParts(value) {
        this._engHiliParts = value;
    }

    get chiHiliParts() {
        return this._chiHiliParts;
    }

    set chiHiliParts(value) {
        this._chiHiliParts = value;
    }

    // @Expose()
    get pickDoneFormsCountCheckedListMap() {
        return this._pickDoneFormsCountCheckedListMap;
    }

    @Expose()
    get pickDoneFormsCountCheckedListMapForPersist(){
        let result = [...this._pickDoneFormsCountCheckedListMap.entries()].map(entry=>new Map([["form",entry[0]],["checkedList",entry[1]]]));
        return result;
    }

    set pickDoneFormsCountCheckedListMapForPersist(value){
        value.forEach(entry=>{
            this._pickDoneFormsCountCheckedListMap.set(entry.form,entry.checkedList);
        })

    }

    set pickDoneFormsCountCheckedListMap(value) {
        this._pickDoneFormsCountCheckedListMap = value;
    }

    pickDoneFormsCountsHas(inputFormCount){
        return [...this._pickDoneFormsCountCheckedListMap.values()].findIndex(theFormCountsArr=>theFormCountsArr.findIndex(theFormCount=>theFormCount===inputFormCount)>=0)>=0;
    }

    isPickDoneFormsCountCheckedListMapEmpty(){
        return this._pickDoneFormsCountCheckedListMap.size===0||[...this._pickDoneFormsCountCheckedListMap.entries()].every(entry=>entry&&entry.length===2&&entry[1]&&entry[1]?.length===0);
    }
    isPickDoneFormsCountCheckedListMapNotEmpty(){
        return !this.isPickDoneFormsCountCheckedListMapEmpty();
    }

    // set formsCountPickingArr(value) {
    //     this._formsCountPickingArr = value;
    // }

    get inputPatternMatched() {
        return this._engHiliParts.inputPatternMatched||this._chiHiliParts.inputPatternMatched;
    }

    clearAllParts() {
        this._engHiliParts.clearAllParts();
        this._chiHiliParts.clearAllParts();
    }
}

YoudaoLijuPatternMarkingTemp.HilightParts = class {
    constructor(originText) {
        this._originText = originText;
        this._prePartsArr = [];
        this._hilightPartsArr = [];
        this._railPartsArr = [];
        this._toHili = false;
        this._inputPatternMatched = false;
    }


    get originText() {
        return this._originText;
    }

    set originText(value) {
        this._originText = value;
    }

    get prePartsArr() {
        return this._prePartsArr;
    }

    set prePartsArr(value) {
        this._prePartsArr = value;
    }

    get hilightPartsArr() {
        return this._hilightPartsArr;
    }

    set hilightPartsArr(value) {
        this._hilightPartsArr = value;
    }

    get railPartsArr() {
        return this._railPartsArr;
    }

    set railPartsArr(value) {
        this._railPartsArr = value;
    }

    get toHili() {
        return this._toHili;
    }

    set toHili(value) {
        this._toHili = value;
    }


    get inputPatternMatched() {
        return this._inputPatternMatched;
    }

    set inputPatternMatched(value) {
        this._inputPatternMatched = value;
    }

    clearAllParts() {
        this._prePartsArr = [];
        this._hilightPartsArr = [];
        this._railPartsArr = [];
        this._toHili = false;
        this._inputPatternMatched = false;
    }
}

export {YoudaoLijuPatternMarkingTemp,Liju};